#include "head.h"

int insert(MYSQL* mysql, int uid, const char* size, const char* file_path, const char* file, const char* hash, const char* path);
bool ishavehash(MYSQL* mysql, const char* hash);
bool ishave(MYSQL* mysql, const char* file_path, int uid);
int mysql_alter(MYSQL* mysql, int uid, const char* file_path, int file_size);
// up
int to_up(messenger_t* msg, int sock_fd, MYSQL* mysql)
{
    
    //char sql[200] = {0};
    // 切割参数
    char* path = msg->args;
    char* file = msg->args;
    while(*file != '&') ++file;
    *file++ = '\0';

    char* hash = file;
    while(*hash != '&') ++hash;
    *hash++ = '\0';

    char* size = hash;
    while(*size != '&') ++size;
    *size++ = '\0';

    char file_path[100] = {0};
    if(strcmp(path, "/") == 0)
        sprintf(file_path, "%s%s", path, file);
    else
        sprintf(file_path, "%s/%s", path, file);

    // 得到用户的id // 解密 TOKEN
    int uid = 0;
    int ret = l8w8jwt_uid(msg->token, &uid);
    if(ret == 1)
    {
        // 解密失败
        int ret = -1;
        send(sock_fd, &ret, sizeof(int), MSG_NOSIGNAL);
        close(sock_fd);
        return 0;
    }

    printf("%s, %s, %s, %d\n", file_path, file, hash, atoi(size));
    // 先查当前路径有没有这个文件
    if(ishave(mysql, file_path, uid))
    {
        // 存在同名文件 返回 -1
        int ret = -1;
        send(sock_fd, &ret, sizeof(int), MSG_NOSIGNAL);
        return 0;
    }

    // 当前路径下不存在同名文件
    // 查找是否有同哈希文件
    if(ishavehash(mysql, hash))
    {
        // 有同 hash 的完整的文件 返回 -2
        int ret = -2;
        send(sock_fd, &ret, sizeof(int), MSG_NOSIGNAL);

        // 插入一条记录
        insert(mysql, uid, size, file_path, file, hash, path);
        return 0;
    }


    // 插入一条记录
    insert(mysql, uid, size, file_path, file, hash, path);

    // 按照hash值直接打开文件
    char pathh[1024] = {0};
    getparameter("path", pathh);
    strcat(pathh, hash);
    int file_fd = open(pathh, O_WRONLY | O_CREAT | O_APPEND, 0666);

    // 计算文件的大小
    struct stat file_stat;
    fstat(file_fd,&file_stat);
    int file_size = file_stat.st_size;
    // 发送现在的文件大小
    send(sock_fd, &file_size, sizeof(int), MSG_NOSIGNAL);

    // ftruncate(file_fd, atoi(size));
    while(1)
    {
        char buf[4096] = {0};
        int count = recv(sock_fd, buf, sizeof(buf), 0);
        printf("\rcount = %d", count);
        fflush(stdout);
        file_size += count;
        write(file_fd, buf, count);
        if(count == 0 && file_size != atoi(size))
        {
            // 没传完
            ftruncate(file_fd, file_size);
            // 修改 为不完整
            //////////////////////////////////
            mysql_alter(mysql, uid, file_path, file_size);
            printf("没传完\n");
            break;
        }
        else if(count == 0 && file_size == atoi(size))
        {
            // 传完了
            printf("传完了\n");
            break;
        }

    }
    close(file_fd);
    close(sock_fd);
    printf("接收完成\n");
    return 0;
}


// 先查当前路径有没有这个文件
bool ishave(MYSQL* mysql, const char* file_path, int uid)
{
    char sql[200] = {0};
    sprintf(sql, "select * from file where file_usr = %d and file_path = '%s'", uid, file_path);
    LOG_MSG(sql);
    if(mysql_query(mysql, sql) != 0)
        LOG_MSG("mysql_query");
    
    MYSQL_RES* res = mysql_store_result(mysql);
    if(res != NULL && mysql_num_rows(res) != 0)
        return true;
    else
        return false;
}

// 查找是否有同哈希文件
bool ishavehash(MYSQL* mysql, const char* hash)
{
    char sql[1024] = {0};
    sprintf(sql, "select * from file where file_hash = '%s' and file_exist = 0 and file_intact = 0", hash);
    LOG_MSG(sql);
    if(mysql_query(mysql, sql) != 0)
        LOG_MSG("mysql_query");
    
    MYSQL_RES* res = mysql_store_result(mysql);
    if(res != NULL && mysql_num_rows(res) != 0)
        return true;
    else
        return false;
}


int insert(MYSQL* mysql, int uid, const char* size, const char* file_path, const char* file, const char* hash, const char* path)
{
    char sql[1024] = {0};

    // 找父目录 id
    sprintf(sql, "select * from file where file_usr = %d and file_path = '%s' and file_exist = 0", uid, path);
    LOG_MSG(sql);
    if(mysql_query(mysql, sql) != 0)
        LOG_MSG("mysql_query");
    MYSQL_RES* res = mysql_store_result(mysql);
    ERROR_CHECK(res, NULL, "mysql_store_result");
    if(mysql_num_rows(res) == 0 || res == NULL)
    {
        LOG_MSG("mysql_store_result");
        mysql_free_result(res);
        return -1;
    }

    MYSQL_ROW line = mysql_fetch_row(res);
    // 得到路径id
    int father_id = atoi(line[0]);
    mysql_free_result(res);

    bzero(sql, sizeof(sql));
    sprintf(sql, "insert into file values (NULL, '%s', %d, '%s', 0, '%s', 0, %d, 0, %d)", file_path, uid, file, hash, father_id, atoi(size));
    LOG_MSG(sql);
    if(mysql_query(mysql, sql) != 0)
        LOG_MSG("mysql_query");


    LOG_MSG("增加文件\n");
    return 0;
}

int mysql_alter(MYSQL* mysql, int uid, const char* file_path, int file_size)
{
    char sql[200] ={0};
    sprintf(sql, "update file set file_intact = 1, file_size = %d where file_usr = %d and file_path = %s", file_size, uid, file_path);
    LOG_MSG(sql);
    if(mysql_query(mysql, sql) != 0)
        LOG_MSG("mysql_query");

    return 0;
}